<?php
/**
 * ***********************************************************************************
 * 咨询鲤 PC端知识店铺-资讯相关
 * 
 * @author Mr.RJH
 * @date 2025-09-09
 * @email<15213026209@163.com>
 * ***********************************************************************************
 */
namespace app\controller;

use app\BaseController;

use app\model\article\ZxlKnowStoreArticleModel;
use app\model\product\ZxlKnowStoreCategoryModel;
use app\model\product\ZxlKnowStoreProductModel;

use app\service\ProductService;

class Article extends BaseController
{
    /**
     * 资讯列表
     * 
     * @return mixed
     */
    public function index()
    {
        
        if ($this->request->isPost()) {

            $limit = $this->request->param('limit');
            $page = $this->request->param('page',1);
            $cate_id = $this->request->param('category_id',0);
            
            $m_id = $this->request->m_id;
            try {
                //查询条件
                $where = function($q)use($m_id,$cate_id){
                    $q->where('m_id', $m_id);
                    $q->where('is_delete', 0);
                    $q->where('status', 1);
                    if ($cate_id > 0) {
                        $q->where('cate_id', $cate_id);
                    }
                };
                $query = ZxlKnowStoreArticleModel::where($where);
                $list = $query->order(['sort' => 'desc','create_time' => 'desc'])->paginate($limit,false,['page'=>$page])->toArray();
                foreach ($list['data'] as &$v) {
                    $two_cate = ZxlKnowStoreCategoryModel::where(['id' => $v['cate_id']])->find();
                    $first_name = ZxlKnowStoreCategoryModel::where(['id' => $two_cate['parent_id']])->value('name');
                    $v['cat_names'] = $first_name.'<span lay-separator="" style="margin:5px;color:#c2c2c2;">/</span>'.$two_cate['name'];
                    $v['create_time'] = date('m月d日 H:i');
                }
            } catch (\Exception $e) {
                return json(['code' => 1,'msg' => $e->getMessage()]);
            }

            return json(['code' => 0,'msg' => 'SUCCESS','count' => $list['total'],'data' => $list['data']]);
        }

        // 获取分类
        $first_category = ZxlKnowStoreCategoryModel::first_category($this->request->m_id, 7);
        $second_category = [];
        if ($first_category) {
            $second_category = ZxlKnowStoreCategoryModel::second_category($this->request->m_id, $first_category[0]['id']);
        }
        $productService = new ProductService($this->request->m_id);
        // 推荐课程
        $course_list = $productService->recommend_product(3, 4);
        
        $this->assign('course_list', $course_list);
        $this->assign('second_category', $second_category);
        return $this->fetch();
    }

    /**
     * 文章详情
     * 
     * @return mixed
     */
    public function detail()
    {
        $id = $this->request->param('id');
        $info = ZxlKnowStoreArticleModel::where(['m_id' => $this->request->m_id, 'id' => $id, 'is_delete' => 0])->find();
        if (!$info) {
            return redirect('/error/http404');
        }
        $info->click_num += 1;
        $info->save();
        $info['create_time'] = date('m月d日', $info['create_time']);
        $info['recommend_list'] = [];
        if ($info['recommends']) {
            $recommend_list = ZxlKnowStoreProductModel::with("attrMinPrice")->whereIn('id', $info['recommends'])
                            ->whereIn('product_type',ZxlKnowStoreProductModel::PC_PRODUCTTYPE)
                            ->field('id,name,sub_title,product_type,head_imgs,background_color,real_sales,virtual_sales,tags')
                            ->select()->toArray();

            foreach ($recommend_list as &$v) {
                if ($v['product_type'] == 10) {
                    $v['head_imgs'] = '/static/images/exam.png';
                } else {
                    $v['head_imgs'] = $v['head_imgs'] ? explode(',',$v['head_imgs'])[0] : '/static/images/no_pic_1.png';
                }
                $v['product_type_text'] = ZxlKnowStoreProductModel::getPcProductType($v['product_type']);
                $v['href'] = ZxlKnowStoreProductModel::getPcProductDetailHref($v['product_type'], $v['id']);
                
            }
            $info['recommend_list'] = $recommend_list;
        }

        $productService = new ProductService($this->request->m_id);
        // 推荐实物
        $goods_list = $productService->recommend_product(1, 6);

        $this->assign('goods_list', $goods_list);
        $this->assign('info', $info);
        return $this->fetch();
    }
}